# coding=utf-8
##
## Author: jmdvirus@aliyun.com
##
## Create: 2020年12月10日 星期四 15时08分50秒
##

import requests
import json
import smtplib

def read_config(conffile):
    with open(conffile) as f:
        data = json.loads(f.read())
        return data

def send_webhook(hook, data):
    post_data = json.dumps(data)
    headers = {'Content-Type': 'application/json; charset=utf-8'}
    r = requests.post(hook, headers=headers, data=post_data)
    print("send result: ", r.status_code)

def send_webhook_jmdkina(data):
    c = read_config("/opt/data/todocker/etc/check.json")
    send_webhook(c["feishu_webhook"], data)

def send_email(to, title, text):
    server = smtplib.SMTP('localhost')
    server.set_debuglevel(1)
    server.sendmail('v@jmd', to, text)
    server.quit()

def send_email_monitor(title, text):
    send_email("ciyingjue@163.com", title, text)

def check_jmdkina():
    url = "https://www.jmdkina.cn"
    r = requests.get(url)
    d = {"msg_type":"text"}
    d["id"] = "jmdkina"
    if r.status_code == 200:
        print("OK")
        dc = {}
        dc["text"] = "jmdkina- Success"
        d["content"] = dc
        send_webhook_jmdkina(d)
    else:
        print("Fail")
        dc = {}
        dc["text"] = "jmdkina- " + url + " connect fail"
        d["content"] = dc
        send_webhook_jmdkina(d)

if __name__ == '__main__':
    check_jmdkina()
    
